package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.bean.Rz;
import com.qf.service.impl.RzServiceImpl;
import com.qf.util.ExcelUtils;
import com.qf.util.ShopExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * JoJoker
 * 李杨帆2019/12/9 16:56
 */
@Controller
public class RzController {
    @Autowired
    private RzServiceImpl rzService;

    @RequestMapping("/findallrz")
    public String findAllRz(@RequestParam(defaultValue = "1") int index,
                            ModelMap map, @RequestParam(defaultValue = "") String jgname, @RequestParam(defaultValue = "") String jglx) {
        PageInfo<Rz> rz = rzService.findRz(index, 5, jgname, jglx);
        map.addAttribute("pi", rz);
        map.addAttribute("jgname", jgname);
        map.addAttribute("jglx", jglx);
        return "showRz";
    }

    @RequestMapping("/addrz")
    public String addRz(Rz rz, HttpServletResponse response) throws Exception{
        rz.setRzdate(new Date());
        int i = rzService.addRz(rz);
        if(i>0){
            //新增成功
            return "redirect:/findallrz";
        }else {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter writer = response.getWriter();
            writer.print("<script>alert('add failed 添加失败');location.href='/findallrz'</script>");
        }
        return null;
    }

    @RequestMapping("/updaterz")
    public String updateRz(Rz rz, HttpServletResponse response)throws Exception {
        rz.setRzdate(new Date());
        System.out.println(rz.toString());
        int i = rzService.updateRz(rz);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        if (i > 0) {

            writer.print("<script>alert('Update successful 成功');location.href='/findallrz'</script>");
        } else {
            writer.print("<script>alert('Update failed 失败');location.href='/findallrz'</script>");
        }
        return null;
    }

    @RequestMapping("/deleterz")
    public String deleteRz(int rzid, HttpServletResponse response) throws Exception {
        int i = rzService.deleteRzByrzid(rzid);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        if (i > 0) {

            writer.print("<script>alert('Delete successful 成功');location.href='/findallrz'</script>");
        } else {
            writer.print("<script>alert('Delete failed 失败');location.href='/findallrz'</script>");
        }
        return null;
    }

    @RequestMapping("/findbyrzid")
    public String findbyRzid(int rzid,ModelMap map) {
        Rz rzbyrzid = rzService.findRzbyrzid(rzid);
        map.addAttribute( "rz", rzbyrzid);
        return "updateRz";
    }

    @RequestMapping("/deleterzs")
    public void deleteRzs(int[] rzid, HttpServletResponse response, String method){
        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter writer = response.getWriter();
        if (method.equals("delete")) {
            int i = rzService.deleteRzs(rzid);
            if (i > 0) {

                writer.print("<script>alert('Delete successful 成功');location.href='/findallrz'</script>");
            } else {
                writer.print("<script>alert('Delete failed 失败');location.href='/findallrz'</script>");
            }
        }else if (method.equals("daochu")) {
            try {
                //导出数据
                //1.查询被选中的数据信息
                List<Rz> rzList = rzService.findByRzids(rzid);
                for (Rz rz : rzList) {
                }
                //2.导出数据
                ExcelUtils.createFirst("日志信息");
                ExcelUtils.createOthers(rzList);
                SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
                ShopExcelUtils.export(new FileOutputStream("D://atm//" + (format.format(new Date())) + ".xls"));
                writer.print("<script>alert('导出成功');location.href='/findallrz'</script>");
            } catch (IOException e) {
                e.printStackTrace();
                }
            }
        } catch (IOException e) {
        e.printStackTrace();
        }
    }

    @RequestMapping("/login.html")
    public String test(){
        return "login";
    }
    @RequestMapping("/head.html")
    public String test2(){
        return "head";
    }

    @RequestMapping("/index.html")
    public String test3(){
        return "index";
    }
    @RequestMapping("/left.html")
    public String test04(){
        return "left";
    }
    @RequestMapping("/right.html")
    public String test05(){
        return "right";
    }
    @RequestMapping("/add.html")
    public String test6() { return "addRz";}
    @RequestMapping("/up.html")
    public String test7() { return "updateRz2";}
}
